package org.cloud.im.server.protocol.http.api;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.cloud.im.core.http.HttpRequest;
import org.cloud.im.core.http.HttpResponse;
import org.cloud.im.server.protocol.http.annotation.RequestPath;
import org.cloud.im.server.util.HttpResps;

import java.io.File;

/**
 * @Author: w.weidong.lin@abite.com
 * @Description: TODO
 * @Date: create in 2021/10/19 14:39
 */
@RequestPath(value = "/webim")
public class WebImController {

	public HttpResponse webim(HttpRequest request) throws Exception {
		String root = FileUtil.getAbsolutePath(request.getHttpConfig().getPageRoot());
		String path = request.getRequestLine().getPath();
		File file = new File(root + path);
		if (!file.exists() || file.isDirectory()) {
			if (StringUtils.endsWith(path, "/")) {
				path = path + "index.html";
			} else {
				path = path + "/index.html";
			}
			file = new File(root, path);
		}
		HttpResponse ret = HttpResps.file(request, file);
		return ret;
	}
}
